package com.tjsinfo.tjsoa.addresslist.bo;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.addresslist.dao.IAddressListGroupDAO;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListGroup;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListUser;
import com.tjsinfo.tjsoa.addresslist.vo.AddresslistSend;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class AddressListGroupBo implements IAddressListGroupBo{
	private IQbcDao qbcDao;
	private IJDBCDao dao;
	private IAddressListGroupDAO listGroupDAO;
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#getQbcDao()
	 */
	public IQbcDao getQbcDao() {
		return qbcDao;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#setQbcDao(com.tjsinfo.tjsoa.Dao.IQbcDao)
	 */
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#getListGroupDAO()
	 */
	public IAddressListGroupDAO getListGroupDAO() {
		return listGroupDAO;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#setListGroupDAO(com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupDAO)
	 */
	public void setListGroupDAO(IAddressListGroupDAO listGroupDAO) {
		this.listGroupDAO = listGroupDAO;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#getDao()
	 */
	public IJDBCDao getDao() {
		return dao;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#setDao(com.tjsinfo.tjsoa.Dao.IJDBCDao)
	 */
	public void setDao(IJDBCDao dao) {
		this.dao = dao;
	}
	
	
	private static List	deleteList;
	public static List getDeleteList() {
		return deleteList; 
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#getPrivateGroup(int)
	 */
	public String[] getGroup(int user_id,String type) {
		SimpleExpression a = Restrictions.eq("moduleType", Short.parseShort(type));
		DetachedCriteria criteria = DetachedCriteria.forClass(AddressListGroup.class);
		criteria.add(a);
		criteria.addOrder(Order.asc("groupNo"));
		
		if (type.equals("1")) {
			DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
			SimpleExpression b = Restrictions.eq("id", user_id);
			criteria1.add(b);
		}
		
		List<AddressListGroup> list = qbcDao.getCriteria(criteria); 
		
		String[] strings = new String[list.size()*3];
		int i = 0;
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			AddressListGroup group = (AddressListGroup) iterator.next();
			strings[i] = group.getId().toString();
			i++;
			strings[i] = group.getGroupName();
			i++;
			if(group.getGroupNo()==null){
				strings[i] = "1";
			}else {
				strings[i] = group.getGroupNo().toString();
			}
			i++;
		}
		return strings;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#insertGroup(java.lang.String, int, java.lang.String)
	 */
	public String insertGroup(String groupName,int user_id,String moduleType,int groupNo) {
		AddressListGroup group = new AddressListGroup();
		if (moduleType.equals("1")) {
			TSystemUser user = new TSystemUser();
			user.setUserId(user_id);
			group.setTjsUser(user);
		}
		group.setGroupNo(groupNo);
		group.setGroupName(groupName);
		group.setModuleType(Short.valueOf(moduleType));
		group.setCreateTime(new Date());
		
		String str = "error";
		try {
			listGroupDAO.save(group);
			str = "ok";
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#updateGroup(int, java.lang.String, int, java.lang.String)
	 */
	public String updateGroup(String id,String groupName,int user_id,String moduleType,int groupNo) {
		AddressListGroup group = listGroupDAO.findById(id);
		group.setGroupName(groupName);
		group.setGroupNo(groupNo);
		
		String str = "error";
		try {
			listGroupDAO.update(group);
			str = "ok";
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#FirstDeleteGroup(int)
	 */
	public String FirstDeleteGroup(String groupid) {
		SimpleExpression a = Restrictions.eq("id", groupid);
		DetachedCriteria criteria = DetachedCriteria.forClass(AddressListGroup.class);
		criteria.add(a);
		
		ArrayList<AddressListGroup> list = (ArrayList)qbcDao.getCriteria(criteria);
		AddressListGroup group = list.iterator().next();
		SimpleExpression b = Restrictions.eq("addressListGroup", group);
		DetachedCriteria criteria1 = DetachedCriteria.forClass(AddressListUser.class);
		criteria1.add(b);
		ArrayList<AddressListUser> list1 = (ArrayList)qbcDao.getCriteria(criteria1);
		String str = "ok";
		if(list1.size() != 0){
			str = "haveUser";
			Object[] obj = new Object[list1.size()];
			int i = 0;
			for (Iterator iterator = list1.iterator(); iterator.hasNext();) {
				AddressListUser user = (AddressListUser) iterator.next();
				obj[i] = user;
				i++;
			}
			
			Criterion c = Restrictions.in("addressListUser", obj);
			DetachedCriteria criteria2 = DetachedCriteria.forClass(AddresslistSend.class);
			criteria2.add(c);
			ArrayList<AddressListUser> list2 = (ArrayList)qbcDao.getCriteria(criteria2);
			if (list2.size() != 0) {
				deleteList = list2;
				str = "error";
			}
		}else {
			this.deleteGroup(groupid);
		}
		return str;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#deleteGroup(int)
	 */
	public String deleteGroup(String id) {
		SimpleExpression a = Restrictions.eq("id", id);
		DetachedCriteria criteria = DetachedCriteria.forClass(AddressListGroup.class);
		criteria.add(a);
		
		ArrayList<AddressListGroup> list = (ArrayList)qbcDao.getCriteria(criteria);
		AddressListGroup group = list.iterator().next();
		
		listGroupDAO.delete(group);
		return "ok";
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_Group.IAddressListGroupBo#findById(int)
	 */
	public AddressListGroup findById(String id) {
		AddressListGroup group = listGroupDAO.findById(id);
		return group;
	}
}
